#!/bin/bash
###
 # @Author: dvlproad
 # @Date: 2023-06-07 16:03:56
 # @LastEditors: dvlproad
 # @LastEditTime: 2023-06-08 19:00:18
 # @Description: 
### 
#sh uploadArgSetForEnv_all.sh -appInfoF ${FILE_PATH} --pgyerUploadUrlType ${PgyerUploadUrlType} --shouldBackupToCos ${ShouldBackupToCos} --shouldUploadToAppStore ${ShouldUploadToAppStore}
#sh uploadArgSetForEnv_all.sh -appInfoF "../example_packing_info/app_info.json" --pgyerUploadUrlType true --shouldBackupToCos false --shouldUploadToAppStore false

# 当前【shell脚本】的工作目录
# $PWD代表获取当前路径，当cd后，$PWD也会跟着更新到新的cd路径。这个和在终端操作是一样的道理的
CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"
#echo "CurrentDIR_Script_Absolute=${CurrentDIR_Script_Absolute}"
#CommonFun_HomeDir_Absolute=${CurrentDIR_Script_Absolute}/..
CommonFun_HomeDir_Absolute=${CurrentDIR_Script_Absolute%/*} # 使用此方法可以避免路径上有..

qscript_path_get_filepath="${CommonFun_HomeDir_Absolute}/qscript_path_get.sh"
qbase_update_json_file_singleString_script_path="$(sh ${qscript_path_get_filepath} qbase update_json_file_singleString)"


#echo "===============进入脚本:$0"
# 包来源分支
# shell 参数具名化
show_usage="args: [-appInfoF, -pgyerUploadUrlType, -supCos, -supAppStore]\
                                  [--app-info-json-file=, --pgyerUploadUrlType=,--shouldBackupToCos=,--shouldUploadToAppStore=]"

while [ -n "$1" ]
do
        case "$1" in
                -appInfoF|--app-info-json-file) FILE_PATH=$2; shift 2;;
                -pgyerUploadUrlType|--pgyerUploadUrlType) PgyerUploadUrlType=$2; shift 2;;
                -supCos|--shouldBackupToCos) ShouldBackupToCos=$2; shift 2;;
                -supAppStore|--shouldUploadToAppStore) ShouldUploadToAppStore=$2; shift 2;;
                --) break ;;
                *) echo $1,$2,$show_usage; break ;;
        esac
done

JQ_EXEC=`which jq`

if [ ! -f "${FILE_PATH}" ];then
    echo "$0 ❌:您的$PWD/${FILE_PATH}文件不存在，请检查！"
    exit_script
fi

source ${CommonFun_HomeDir_Absolute}/a_function.sh ${CommonFun_HomeDir_Absolute}
check_jsonFile_valid ${FILE_PATH}

# 上传到蒲公英的类型(没上传、上传到待发布、上传到直接发布)
if [ -n "${PgyerUploadUrlType}" ]; then
    echo "PgyerUploadUrlType=${PgyerUploadUrlType}"
    sh ${qbase_update_json_file_singleString_script_path} -jsonF "${FILE_PATH}" -k "package_result.pgyerUploadUrlType" -v "${PgyerUploadUrlType}"
    if [ $? != 0 ]; then
        exit_script
    fi
fi

# 是否上传/备份到cos
if [ -n "${ShouldBackupToCos}" ]; then
    echo "ShouldBackupToCos=${ShouldBackupToCos}"
    #sed -i '' "s/no upload cos/${ShouldBackupToCos}/g" ${FILE_PATH}
    sh ${qbase_update_json_file_singleString_script_path} -jsonF "${FILE_PATH}" -k "package_result.shoudUploadToCos" -v "${ShouldBackupToCos}"
fi

# 是否上传到appStore
if [ -n "${ShouldUploadToAppStore}" ]; then
    echo "ShouldUploadToAppStore=${ShouldUploadToAppStore}"
    #sed -i '' "s/no upload appstore/${ShouldUploadToAppStore}/g" ${FILE_PATH}
    sh ${qbase_update_json_file_singleString_script_path} -jsonF "${FILE_PATH}" -k "package_result.shoudUploadToAppStrore" -v "${ShouldUploadToAppStore}"
fi

